package com.frxuexi.park.view

import android.widget.SearchView.OnQueryTextListener
import androidx.activity.viewModels
import androidx.core.view.GravityCompat
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.paging.PagingData
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.frxuexi.base.entity.ParkBeanItem
import com.frxuexi.base.mvi.BaseActivity
import com.frxuexi.base.mvi.RequestType
import com.frxuexi.base.mvi.UiState
import com.frxuexi.base.utils.ArouterManager
import com.frxuexi.park.OnItemClickListener
import com.frxuexi.park.OnItemLongClickListener
import com.frxuexi.park.ParkAdapter
import com.frxuexi.park.ParkAdapter2
import com.frxuexi.park.ParkIntent
import com.frxuexi.park.ParkViewModel
import com.frxuexi.park.R
import com.frxuexi.park.databinding.ActivityParkBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch

@Route(path = "/park/ParkActivity")
@AndroidEntryPoint
class ParkActivity : BaseActivity<ActivityParkBinding, ParkIntent, ParkViewModel>() {

//    val parkAdapter by lazy { ParkAdapter() }
    val parkAdapter2 by lazy { ParkAdapter2() }

    override fun getVM(): ParkViewModel {
        return viewModels<ParkViewModel>().value
    }

    override fun getBind(): ActivityParkBinding {
        return ActivityParkBinding.inflate(layoutInflater)
    }

    override fun initData() {
        lifecycleScope.launch {
            lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
                vm.uiState.collectLatest {
                    when (it) {
                        is UiState.onSuccess<*> -> {
                            when (it.type) {
                                RequestType.DELETE -> {
                                    vm.sendIntent(ParkIntent.getParkByType("标准车位"))
                                }

                                RequestType.DEFAULT -> {
//                                    val items = it.data as PagingData<ParkBeanItem>
//                                    parkAdapter.submitData(lifecycle, items)
                                    val items = it.data as Collection<ParkBeanItem>
                                    parkAdapter2.apply {
                                        data.clear()
                                        addData(items)
                                        notifyDataSetChanged()
                                    }
                                }
                                RequestType.ITEM->{
                                    val items = it.data as ParkBeanItem
                                    parkAdapter2.apply {
                                        data.clear()
                                        addData(items)
                                        notifyDataSetChanged()
                                    }
                                }

                                else -> {}
                            }

                        }


                        else -> {}
                    }
                }
            }
        }
    }

    override fun initView() {

        binding.headPark.headAdd.setOnClickListener {
            ArouterManager.navigation(this, "/park/ParkAddActivity", "pid")
        }

        binding.parkRv.apply {
            adapter = parkAdapter2
            layoutManager = LinearLayoutManager(this@ParkActivity)
        }


        binding.parkFilter.setOnClickListener {
            binding.parkDrawer.openDrawer(GravityCompat.END)
        }

        binding.parkRgstatus.setOnCheckedChangeListener { group, checkedId ->
            var status = "空闲"
            when (checkedId) {
                R.id.rb_statuskx -> status = "空闲"
                R.id.rb_stauszy -> status = "已占用"
                R.id.rb_stauswh -> status = "维护中"

            }
            vm.sendIntent(ParkIntent.getParkByStatus(status))
            binding.parkDrawer.closeDrawer(GravityCompat.END)
        }

        binding.parkRgtype.setOnCheckedChangeListener { group, checkedId ->
            var type = "标准车位"
            when (checkedId) {
                R.id.rb_typea -> type = "标准车位"
                R.id.rb_typeb -> type = "VIP车位"
            }
            vm.sendIntent(ParkIntent.getParkByType(type))
            binding.parkDrawer.closeDrawer(GravityCompat.END)
        }

        binding.parkSv.setOnQueryTextListener(object : OnQueryTextListener {
            override fun onQueryTextSubmit(query: String?): Boolean {
//                vm.sendIntent(ParkIntent.getParkByArea(query!!))
//                vm.sendIntent(ParkIntent.getParkByUid(query!!))
                vm.sendIntent(ParkIntent.getParkByCarNo(query!!))

                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                return true
            }

        })
        parkAdapter2.setOnItemClickListener { adapter, view, position ->
            ArouterManager.navigation(
                this@ParkActivity,
                "/park/ParkAddActivity",
                "pid",
                parkAdapter2.data.get(position).id.toString()
            )
        }

        parkAdapter2.setOnItemLongClickListener { adapter, view, position ->
            ArouterManager.navigation(this@ParkActivity, "/park/ParkVRActivity")
            return@setOnItemLongClickListener true
        }

        /*parkAdapter.setOnItemLongClickListener(object : OnItemLongClickListener {
            override fun onItemLongClick(): Boolean {
                ArouterManager.navigation(this@ParkActivity, "/park/ParkVRActivity")
                return true
            }

        })

        parkAdapter.setOnItemClickListener(object : OnItemClickListener {
            override fun onItemClick(id: Int) {
                ArouterManager.navigation(
                    this@ParkActivity,
                    "/park/ParkAddActivity",
                    "pid",
                    id.toString()
                )
            }
        })*/

        ItemTouchHelper(object :
            ItemTouchHelper.SimpleCallback(ItemTouchHelper.LEFT, ItemTouchHelper.LEFT) {
            override fun onMove(
                recyclerView: RecyclerView,
                viewHolder: RecyclerView.ViewHolder,
                target: RecyclerView.ViewHolder
            ): Boolean {
                return true
            }

            override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
                val position = viewHolder.adapterPosition
//                vm.sendIntent(ParkIntent.deletePark(parkAdapter.snapshot().items.get(position).id!!))
                vm.sendIntent(ParkIntent.deletePark(parkAdapter2.data.get(position).id!!))
            }

        }).attachToRecyclerView(binding.parkRv)

    }

    override fun onResume() {
        super.onResume()
        vm.sendIntent(ParkIntent.getParkByType("标准车位"))
    }

}